﻿//
// Copyright (c) 2024 BPer
//
#include <bperice/BPerRtcSocket.h>
#include <bperice/BPerRtcStun.h>

#include <bperp2p/BPerP2pConnection.h>
#include <bperrtc/BPerRtcConnection.h>

#include <bpersdp/BPerSdp.h>
#include <bperutil/BPerType.h>
#include <bperutil/sys/BPerLog.h>
#include <bperutil/sys/BPerCString.h>
#include <bperutil/sys/BPerHttp.h>
typedef struct{
	int32_t retcode;
		char* sdp;
		char* id;
}P2pSdpResponseType;


#define BPer_SDP_BUFFERLEN 1024*12

void bPer_destroy_p2presponse(P2pSdpResponseType* p2p){
	if(p2p==NULL) return;
	bPer_free(p2p->sdp);
	bPer_free(p2p->id);
}

int32_t bPer_sdp_queryP2p(P2pSdpResponseType* p2p,BPerIpFamilyType familyType,char* ip,int32_t port,char* purl, char* psdp)
{

	int32_t err=BPer_Ok;

	char* sdp=(char*)bPer_calloc(1,BPer_SDP_BUFFERLEN);
	if(bPer_http_post(BPerFalse,familyType,sdp,ip, port, purl, (uint8_t*)psdp, bPer_strlen(psdp))){
		bPer_free(sdp);
		return bPer_error_wrap(1,"query p2p sdp failure!");
	}

	char* sBuffer=(char*)bPer_calloc(1,BPer_SDP_BUFFERLEN);

	bPer_cstr_replace(sdp,sBuffer, "{", "");
	bPer_memset(sdp,0,BPer_SDP_BUFFERLEN);
	bPer_strcpy(sdp,sBuffer);
	bPer_memset(sBuffer,0,BPer_SDP_BUFFERLEN);
	bPer_cstr_replace(sdp,sBuffer, "}", "");

	bPer_memset(sdp,0,BPer_SDP_BUFFERLEN);
	bPer_strcpy(sdp,sBuffer);
	bPer_memset(sBuffer,0,BPer_SDP_BUFFERLEN);
	bPer_cstr_replace(sdp,sBuffer, "\\r\\n", "\n");

	BPerStrings strs;
	bPer_memset(&strs,0,sizeof(BPerStrings));
	bPer_cstr_split(sBuffer, (char*)",",&strs);


	char* p=NULL;
	for (int32_t i = 0; i < strs.vsize; i++) {

		if ((p = bPer_strstr(strs.str[i], "\"code\""))) {
			char *buf = (char*) bPer_calloc(1, bPer_strlen(p) + 1);
            bPer_cstr_replace(p + bPer_strlen("\"code\":"), buf, "\"", "");

			p2p->retcode = abs(atoi(buf));
			bPer_free(buf);

			if ((err = p2p->retcode) != 0)	break;

			continue;
		}



		if ((p = bPer_strstr(strs.str[i], "\"id\""))) {
			p2p->id = (char*) bPer_calloc(1, bPer_strlen(p) + 1);
			bPer_cstr_replace(p + bPer_strlen("\"id\" : "), p2p->id, "\"","");
			continue;
		}

		if ((p = bPer_strstr(strs.str[i], "\"sdp\""))) {
			char* sdptmp=(char*) bPer_calloc(1, bPer_strlen(p) + 1);
			p2p->sdp = (char*) bPer_calloc(1, bPer_strlen(p) + 1);

			bPer_cstr_replace(p + bPer_strlen("\"sdp\":"), sdptmp, "\"", "");
			bPer_cstr_replace(sdptmp, p2p->sdp, "\r", "");

			continue;
		}

	}

	bPer_destroy_strings(&strs);
	bPer_free(sdp);

	return err;

}


int32_t bPer_p2p_doHandleSignal(P2pSdpResponseType* p2p,BPerIpFamilyType familyType,char* remoteIp,int32_t remotePort,char* localSdp) {
	int32_t err = BPer_Ok;

	char apiurl[256] ;


	bPer_memset(apiurl,0,sizeof(apiurl));

	bPer_sprintf(apiurl, sizeof(apiurl), "rtc/v1/");
	err=bPer_sdp_queryP2p(p2p,familyType,remoteIp,remotePort,apiurl, localSdp);

	return err;
}

int32_t bPer_p2p_connectRtcServer(BPerIpFamilyType familyType,char* ip,int32_t remotePort,char* localSdp,char* remoteSdp){
	int err=BPer_Ok;

	P2pSdpResponseType sdp;
	bPer_memset(&sdp,0,sizeof(P2pSdpResponseType));

	//char *tsdp=NULL;
	//conn->createOffer(session, &tsdp);
//	tsdp[bPer_strlen(tsdp)]='}';

    if ((err=bPer_p2p_doHandleSignal(&sdp,familyType,ip,remotePort,localSdp))  == BPer_Ok) {
    	bPer_strcpy(remoteSdp,sdp.sdp);
    	//conn->setRemoteDescription(conn->session,sdp.sdp);
	}
    //bPer_free(tsdp);
	bPer_destroy_p2presponse(&sdp);
    return err;
}

